package edu.tms.dao;

import java.util.List;
import java.util.Map;

import edu.tms.bean.Message;
import edu.tms.crud.JdbcUtil;
import edu.tms.dao.common.CommnonDao;

/**
 * 信息记录表dao
 * @author Administrator
 *
 */
@SuppressWarnings("rawtypes")
public class MessageDaoImp implements CommnonDao{
	
	/**
	 * 保存内容
	 * @param value
	 * @return 
	 * @throws Exception
	 */
	@Override
	public long save(Object value) throws Exception {
		//判断类   instanceof 
		if(value instanceof Message){
			Message ms=(Message)value;
			String sql="INSERT INTO t_message(role_name,stu_id,tch_id,message_content,create_time,reading_state,message_state)";
					sql+="VALUE (?,?,?,?,?,?,?)";
			long zhi=JdbcUtil.insertAuto(sql, 
						ms.getRoleName(),
						ms.getStuId(),
						ms.getTchId(),
						ms.getMessageContent(),
						ms.getCreateTime(),
						ms.getReadingState(),
						ms.getMessageState());
			return zhi;
		}
		// TODO Auto-generated method stub
		return 0;
	}
	/**
	 * 修改内容
	 * @param value
	 * @throws Exception
	 */
	@Override
	public int update(Object value) throws Exception {
		if(value instanceof Message){
			Message ms=(Message)value;
			String sql="UPDATE t_message SET message_content=? WHERE message_id=?";
			
			int zhi=JdbcUtil.update(sql,
					ms.getMessageContent(),
					ms.getMessageId());
			return zhi;
		}
		return 0;
	}
	/**
	 * 删除内容
	 * 学生表t_student，教师表t_teacher
	 * @param value
	 * @throws Exception
	 */
	@Override
	public int delete(Object value) throws Exception {
		if(value instanceof Message){
			Message ms=(Message)value;
			
			String sql="DELETE FROM t_message WHERE message_id=?";
			
			int zhi=JdbcUtil.update(sql, 
							ms.getMessageId()
							);
			return zhi;
		}
		return 0;
	}
	/**
	 * 查找所有的内容
	 * 学生表t_student，教师表t_teacher
	 * @param pageNo 第几页
	 * @param pageSize 每页数字
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Message> findAll(int pageNo, int pageSize) throws Exception {
		String sql="SELECT message_id,role_name,stu_name,tch_name,message_content,tm.create_time,reading_state,message_state "; 
		sql+=" FROM t_message tm,t_student ts,t_teacher tt ";
		sql+=" WHERE tm.stu_id=ts.stu_id AND tm.tch_id=tt.tch_id ";
		sql+=" Limit ?,? ";
		
		return JdbcUtil.query(Message.class, sql, (pageNo-1)*pageSize,pageSize);
	}
	
	/**
	 * String 对应  数据库 列名（comment)
	 * Object 对应相应列名值
	 * 返回的是所查找的类中的数据。方便EXCEL导出
	 */
	@Override
	public List<Map<String,Object>> getMapList() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Message> findAll(Object key, int pageNo, int pageSize) throws Exception {
		String sql=" SELECT message_id,role_name,stu_name,tch_name,message_content,tm.create_time,reading_state,message_state "; 
				sql+=" FROM t_message tm,t_student ts,t_teacher tt ";
				sql+=" WHERE tm.stu_id=ts.stu_id AND tm.tch_id=tt.tch_id AND stu_name=? OR tch_name=? ";
				sql+=" LIMIT ?,? ";
		
		return JdbcUtil.query(Message.class, sql, key,key,(pageNo-1)*pageSize,pageSize);
	}
	@Override
	public Object findByKey(Object key) throws Exception {
		return null;
	}

}
